package com.cloud.api.filter;

import com.alibaba.fastjson.JSONObject;
import com.cloud.base.CacheConstant;
import com.cloud.base.ExceptionConstant;
import com.cloud.base.bean.CloudRequest;
import com.cloud.base.bean.CloudResponse;
import com.cloud.base.cache.CloudRedisTemplate;
import com.cloud.base.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Date;

import static com.cloud.base.BizConstant.FIRST_TYPE_ID_1;
import static com.cloud.base.CacheConstant.TASK_WORK_TAKE_LIMIT;
import static com.cloud.base.ExceptionConstant.TASK_DATE_UPPER_LIMIT;

//@Component
public class RequestLimitFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String token = request.getHeaders().getFirst("token");
        if (StringUtils.isNotBlank(token)) {
            CloudRequest cloudRequest = redisTemplate.get(token);
            if (cloudRequest != null) {
                ServerHttpResponse response = exchange.getResponse();
                String key = CacheConstant.LOGIN_EXPIRATION_DATE.concat(cloudRequest.getUserId());
                String expir = redisTemplate.get(key);
                expir = expir == null ? "0" : expir;
                Long expirCount = Long.valueOf(expir);

                if (expirCount >= 10) {
                    String date = DateUtil.dateToStrLong(new Date(expirCount));
                    CloudResponse cloudResponse = new CloudResponse(ExceptionConstant.USER_LOGIN_EXCEPTION, date);
                    DataBuffer buffer = response.bufferFactory().wrap(JSONObject.toJSONBytes(cloudResponse));
                    response.getHeaders().setContentType(request.getHeaders().getContentType());
                    return response.writeWith(Flux.just(buffer));
                }

                String takeKey = TASK_WORK_TAKE_LIMIT
                .concat(String.valueOf(cloudRequest.getUserId()))
                .concat(String.valueOf(FIRST_TYPE_ID_1));
                if (redisTemplate.get(takeKey) != null && request.getURI().getPath().equals("/app/game/date/list")) {
                    CloudResponse cloudResponse = new CloudResponse(TASK_DATE_UPPER_LIMIT,null);
                    DataBuffer buffer = response.bufferFactory().wrap(JSONObject.toJSONBytes(cloudResponse));
                    response.getHeaders().setContentType(request.getHeaders().getContentType());
                    return response.writeWith(Flux.just(buffer));
                }
            }
        }
        return chain.filter(exchange);
    }

    public int getOrder() {
        return -2;
    }

    @Autowired
    private CloudRedisTemplate redisTemplate;
}